package com.example.hrsystem.workflow.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.workflow.model.ProcessDefinition;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 流程定义列表适配器
 * 用于在RecyclerView中显示流程定义列表
 */
public class ProcessDefinitionAdapter extends RecyclerView.Adapter<ProcessDefinitionAdapter.ProcessDefinitionViewHolder> {
    private List<ProcessDefinition> processDefinitions = new ArrayList<>();
    private OnProcessDefinitionClickListener listener;

    @NonNull
    @Override
    public ProcessDefinitionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_process_definition, parent, false);
        return new ProcessDefinitionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProcessDefinitionViewHolder holder, int position) {
        ProcessDefinition processDefinition = processDefinitions.get(position);
        holder.bind(processDefinition);
    }

    @Override
    public int getItemCount() {
        return processDefinitions.size();
    }

    public void setProcessDefinitions(List<ProcessDefinition> processDefinitions) {
        this.processDefinitions = processDefinitions;
        notifyDataSetChanged();
    }

    public void setOnProcessDefinitionClickListener(OnProcessDefinitionClickListener listener) {
        this.listener = listener;
    }

    class ProcessDefinitionViewHolder extends RecyclerView.ViewHolder {
        private TextView nameTextView;
        private TextView descriptionTextView;
        private TextView versionTextView;
        private TextView createTimeTextView;

        public ProcessDefinitionViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTextView = itemView.findViewById(R.id.text_name);
            descriptionTextView = itemView.findViewById(R.id.text_description);
            versionTextView = itemView.findViewById(R.id.text_version);
            createTimeTextView = itemView.findViewById(R.id.text_create_time);

            // 设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (listener != null && position != RecyclerView.NO_POSITION) {
                        listener.onProcessDefinitionClick(processDefinitions.get(position));
                    }
                }
            });
        }

        public void bind(ProcessDefinition processDefinition) {
            nameTextView.setText(processDefinition.getName());
            descriptionTextView.setText(processDefinition.getDescription());
            versionTextView.setText(String.valueOf(processDefinition.getVersion()));
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            createTimeTextView.setText(sdf.format(processDefinition.getCreatedAt()));
        }
    }

    public interface OnProcessDefinitionClickListener {
        void onProcessDefinitionClick(ProcessDefinition processDefinition);
    }
}